package vista;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import modelo.Persona;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import controlador.PersonaControlador;
import org.eclipse.swt.widgets.Combo;


public class BuscarTutor {
	public Shell shell;

	public String resultado;
	private Table table;
	private Label notiLabel;
	private PersonaControlador persctrl;
	private Combo childDniTutorLabel;
	private String[] campos_buscar = {"NOMBRE","APELLIDO"};
	
	public BuscarTutor(Shell shell) {
		super();
		// TODO Auto-generated constructor stub
		this.shell = shell;
	}
	
	
	
	/**
	 * @wbp.parser.entryPoint
	 */
	public String buscarTutor(final Text dniTutor){
//		Display parent = Display.getDefault();
//		shell = new Shell();		
//		shell.setSize(788, 526);
		persctrl = new PersonaControlador();
		final String resultado = "";
		final Shell dialog = new Shell (shell, SWT.PRIMARY_MODAL | SWT.DIALOG_TRIM);		
        dialog.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        dialog.setSize(435, 217);
        dialog.setText("Buscar Tutores");
        dialog.setLayout(new GridLayout(1, false));
        
        Composite buscadorComposite = new Composite(dialog, SWT.NONE);
        GridLayout gl_buscadorComposite = new GridLayout(3, false);
        gl_buscadorComposite.verticalSpacing = 0;
        buscadorComposite.setLayout(gl_buscadorComposite);
        
        // que sea en lugar un combo de NOMBRE|APELLIDO
        childDniTutorLabel = new Combo(buscadorComposite, SWT.NONE | SWT.READ_ONLY);
        childDniTutorLabel.setItems(campos_buscar);
        childDniTutorLabel.select(0);

        final Text childText = new Text(buscadorComposite, SWT.BORDER);
        childText.setTextLimit(8);
        

        Button buscarButton = new Button (buscadorComposite, SWT.PUSH);
        buscarButton.setText ("Buscar");
        buscarButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	if(childText.getText().length() == 0){
					notiLabel.setText("Debe ingresar al menos un valor");
					notiLabel.pack();
				} else {
					BuscarTutor.this.inicializarTabla(childText.getText());
				}               

            }
        });
        
       
        dialog.setDefaultButton (buscarButton);
        
        table = new Table(dialog, SWT.BORDER | SWT.FULL_SELECTION);
        GridData gd_table = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_table.heightHint = 85;
        table.setLayoutData(gd_table);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        
        TableColumn tblclmnNombre = new TableColumn(table, SWT.NONE);
        tblclmnNombre.setWidth(100);
        tblclmnNombre.setText("Nombre");
        
        TableColumn tblclmnApellido = new TableColumn(table, SWT.NONE);
        tblclmnApellido.setWidth(100);
        tblclmnApellido.setText("Apellido");
        
        TableColumn tblclmnDni = new TableColumn(table, SWT.NONE);
        tblclmnDni.setWidth(100);
        tblclmnDni.setText("Dni");
        
        TableColumn tblclmnCuil = new TableColumn(table, SWT.NONE);
        tblclmnCuil.setWidth(100);
        tblclmnCuil.setText("Cuil");
        
               
        Composite aceptarComposite = new Composite(dialog, SWT.NONE);
        GridData gd_aceptarComposite = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
        gd_aceptarComposite.widthHint = 421;
        aceptarComposite.setLayoutData(gd_aceptarComposite);
        aceptarComposite.setLayout(new GridLayout(3, false));
        
        notiLabel = new Label(aceptarComposite, SWT.NONE);
        GridData gd_notiLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_notiLabel.widthHint = 290;
        notiLabel.setLayoutData(gd_notiLabel);
        
        Button btnCancelar = new Button(aceptarComposite, SWT.RIGHT);
        btnCancelar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        btnCancelar.setText("Cancelar");
        btnCancelar.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	BuscarTutor.this.resultado = "";
				dialog.close();               
            }
        });
        
        
        Button btnAceptar = new Button(aceptarComposite, SWT.RIGHT);
        btnAceptar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        btnAceptar.setText("Aceptar");
         
        
        dialog.addShellListener(new ShellListener() {        	
        	@Override
            public void shellClosed(ShellEvent e) {
        		int filaSelected = table.getSelectionIndex();
        		
				if(filaSelected >= 0 ){
					dniTutor.setText(BuscarTutor.this.resultado);
        		}        		
            }
			@Override
			public void shellActivated(ShellEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void shellDeactivated(ShellEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void shellDeiconified(ShellEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void shellIconified(ShellEvent arg0) {
				// TODO Auto-generated method stub
				
			}       			
        });
        
        btnAceptar.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	int filaSelected = table.getSelectionIndex();
				if(filaSelected >= 0){					
					BuscarTutor.this.resultado = table.getItem(table.getSelectionIndex()).getText(2);
					dialog.close();
				}else{
					notiLabel.setText("Debe seleccionar un Tutor de la lista!");					
					notiLabel.pack();
				}
                
            }
        });
        dialog.open ();
        return resultado;				
	
	}
	
	

	private void inicializarTabla(String valor){
		table.removeAll();
		Map <String,String> map = new HashMap<String, String>();
		map.put("cargo", "TUTOR");
		map.put(childDniTutorLabel.getText().toLowerCase(), valor);
		
		List<Object> personas = persctrl.listarPersonasXCampos(map);		
		for (Object persona : personas) {
			TableItem item = new TableItem(table, SWT.NONE, 0);
			item = personaAItemTable((Persona) persona, item);			
		}		
	}
	
	private TableItem personaAItemTable(Persona per, TableItem item){
		item.setText(0, per.getNombre());
		item.setText(1, per.getApellido());
		item.setText(2, String.valueOf(per.getDni()));
		item.setText(3,per.getCuil());		
		return item;				
}
	
}
